// pages/collector-order/collector-order.js
const request = require('../../utils/request.js')
const api = require('../../config/api.js');
const { stringify, getBarAndTitleHeight } = require('../../utils/utils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarList:[
      {title: '待接单',index: 10 },
      {title: '待上门', index: 40},
      {title: '已关闭', index: 30},
      {title: '已完成', index: 50}
    ],
    activeIndex: 10,
    totalPages:0,
    currentPage:1,
    orderList: [],
    selectId: '',
    // 信号栏高度
    ...getBarAndTitleHeight(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      currentPage: 1
    })
    this.getMyOrderList(1)
  },
    /**
   * 获取我的订单
   */
  async getMyOrderList(currentPage){
    let that = this
    let list = that.data.orderList
    let { activeIndex } = that.data
    if(currentPage ==1){
      list =[];
    }
    wx.showLoading({
      title: '加载中',
    })

    const res = await request.get(api.tempRecycleOrderList,{
      status: activeIndex,
      page: currentPage
    });
    if (res.httpStatus === 200) {
      list = list.concat(res.data.records)
      that.setData({
        orderList:list,
        totalPages:res.data.pages
      })
      wx.hideLoading()
    }
  },

  // 导航栏切换
  navClick(e){
    this.setData({
      currentPage: 1,
      activeIndex: e.currentTarget.dataset.index
    })
    this.getMyOrderList(1)
  },
  // 接单操作
  async handleTake(e){
    if(this.loading) return
    this.loading= true
    const data = await request.post(api.tempRecycleAcceptOrder,{
      id: e.currentTarget.dataset.id
    });
    if (data.httpStatus === 200) {
      wx.showToast({
        title: '接单成功',
        icon: 'none',
      });
      this.setData({
        orderList: [],
        currentPage: 1,
        totalPages: 1,
        activeIndex: 40
      })
      this.getMyOrderList(1)
    }
    if (data.httpStatus === 500) {
      setTimeout(() => {
        this.setData({
          orderList: [],
          currentPage: 1,
          totalPages: 1,
        })
        this.getMyOrderList(1)
      }, 500)
    }
    this.loading= false
  },
  // 关闭订单操作
  async handleClose(e){
    this.setData({
      selectId: e.currentTarget.dataset.id
    })
    this.modal.show()
  },
  // 确认取消
  async toCancle(){
    if(this.loading) return
    this.loading= true
    this.modal.hide()
    const data = await request.post(api.tempRecycleCloseOrder,{
      id: this.data.selectId
    });
    if (data.httpStatus === 200) {
      wx.showToast({
        title: '关闭订单成功',
        icon: 'none',
      });
      this.setData({
        orderList: [],
        currentPage: 1,
        totalPages: 1,
      })
      this.getMyOrderList(1)
    }
    if (data.httpStatus === 500) {
      setTimeout(() => {
        this.setData({
          orderList: [],
          currentPage: 1,
          totalPages: 1,
        })
        this.getMyOrderList(1)
      }, 500)
    }
    this.loading= false
  },
  /**
   * 打电话
   */
  handleCall(e) {
    wx.makePhoneCall({
			phoneNumber: e.currentTarget.dataset.phone,
		});
  },
  /** 
   * 确认订单
  */
 handleConfirm(e){
  let { item }= e.currentTarget.dataset
  let obj= {
    id: item.id,
    selectedList: item.goodsList.map(item=>{
      return {
        id: item.goodsId,
        goodsName: item.goodsName,
        value: item.quantity
      }
    })
  }
  wx.navigateTo({
    url:`/pages/recycleOrderConfirm/recycleOrderConfirm?obj=${stringify(obj)}`
  })
 },
  /**
   * 跳转详情页面
   */
  handleDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/collector-order-detail/collector-order-detail?id=${id}`,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {currentPage,totalPages} = this.data
    if(currentPage<totalPages){
      currentPage++
      this.setData({
        currentPage
      })
      this.getMyOrderList(currentPage)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})